import request from '@/utils/request';

/**
 * 分页查询用户
 */
export async function get_list(params) {
  const res = await request.post('/user/user/get_list', {
    ...params
  });
  if (res.data.code === 1) {
    return {
      list: res.data.data.row_list,
      columns: res.data.data.columns,
      searchData: res.data.data.search_area,
      count: res.data.data.page_info?.total,
      top_button: res.data.data.top_button
    };
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 添加用户
 */
export async function addUser(data) {
  const res = await request.post('/user/user/add', data);
  if (res.data.code === 1) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 修改用户
 */
export async function updateUser(data) {
  const res = await request.post('/user/user/edit', data);
  if (res.data.code === 1) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

export async function role_list(params) {
  const res = await request.post('/user/role/get_list', {
    ...params
  });
  if (res.data.code === 1) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 上传图片
 */
export async function uploadImg(data) {
  const res = await request.post('/system/basic/upload', data);
  if (res.data.code === 1) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}
/**
 * 角色下拉
 */
export async function role_list_select(params) {
  const res = await request.post('/user/role/get_list_select', {
    ...params
  });
  if (res.data.code === 1) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}
/**
 *  获取员工详情
 * @param {user_id} params 用户id
 */
export async function get_user_detail(params) {
  const res = await request.post('/user/user/detail', {
    ...params
  });
  if (res.data.code === 1) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}
